package com.ruoyi.manage.organization.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StrUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import com.ruoyi.manage.organization.domain.OcOrderCredit;
import com.ruoyi.manage.organization.domain.vo.OcOrderCreditVo;
import com.ruoyi.manage.organization.service.IOcOrderCreditService;
import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 订单授信信息Controller
 *
 * @author ruoyi
 * @date 2025-08-19
 */
@Api(tags="订单授信信息控制器")
@RestController
@RequestMapping("/manage/credit" )
public class OcOrderCreditController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IOcOrderCreditService ocOrderCreditService;

    /**
     * 查询订单授信信息列表
     */
    @ApiOperation(value = "查询订单授信信息",notes = "订单授信信息管理")
    @PreAuthorize("@ss.hasPermi('manage:credit:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(OcOrderCreditVo ocOrderCredit,QueryWrapper<OcOrderCredit> queryWrapper) {
        setQueryCondition(ocOrderCredit,queryWrapper);
        startPage();
        List<OcOrderCredit> list = ocOrderCreditService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(OcOrderCreditVo ocOrderCredit, QueryWrapper<OcOrderCredit> queryWrapper) {
        if(ocOrderCredit.getCreditAmount() != null){
            queryWrapper.eq(TABLE_PREFIX+"credit_amount" ,ocOrderCredit.getCreditAmount());
        }
        if(ocOrderCredit.getCreditTime() != null){
            queryWrapper.eq(TABLE_PREFIX+"credit_time" ,ocOrderCredit.getCreditTime());
        }
        if(ocOrderCredit.getRealRate() != null){
            queryWrapper.eq(TABLE_PREFIX+"real_rate" ,ocOrderCredit.getRealRate());
        }
        if(StrUtil.isNotBlank(ocOrderCredit.getPaymentType())){
            queryWrapper.eq(TABLE_PREFIX+"payment_type" ,ocOrderCredit.getPaymentType());
        }
        if(ocOrderCredit.getCreditPeriod() != null){
            queryWrapper.eq(TABLE_PREFIX+"credit_period" ,ocOrderCredit.getCreditPeriod());
        }
        if(StrUtil.isNotBlank(ocOrderCredit.getCreditState())){
            queryWrapper.eq(TABLE_PREFIX+"credit_state" ,ocOrderCredit.getCreditState());
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出订单授信信息列表
     */
    @PreAuthorize("@ss.hasPermi('manage:credit:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, OcOrderCreditVo ocOrderCredit,QueryWrapper<OcOrderCredit> queryWrapper) {
        if (CollUtil.isEmpty( ocOrderCredit.getIds())){
            setQueryCondition(ocOrderCredit,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",ocOrderCredit.getIds());
        }
        List<OcOrderCredit> list = ocOrderCreditService.list(queryWrapper);
        ExcelUtil<OcOrderCredit> util = new ExcelUtil<OcOrderCredit>(OcOrderCredit. class);
        util.exportExcel(response,list, "credit");
    }

    /**
     * 获取订单授信信息详细信息
     */
    @ApiOperation(value = "获取订单授信信息",notes = "订单授信信息管理")
    @PreAuthorize("@ss.hasPermi('manage:credit:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(ocOrderCreditService.queryGetById(id));
    }

    /**
     * 新增订单授信信息
     */
    @ApiOperation(value = "新增订单授信信息",notes = "订单授信信息管理")
//    @PreAuthorize("@ss.hasPermi('manage:credit:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody OcOrderCredit ocOrderCredit) {
        return toAjax(ocOrderCreditService.addOcOrderCredit(ocOrderCredit) ? 1 : 0);
    }

    /**
     * 修改订单授信信息
     */
    @ApiOperation(value = "修改订单授信信息",notes = "订单授信信息管理")
    @PreAuthorize("@ss.hasPermi('manage:credit:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody OcOrderCredit ocOrderCredit) {
        return toAjax(ocOrderCreditService.updateOcOrderCredit(ocOrderCredit) ? 1 : 0);
    }

    /**
     * 删除订单授信信息
     */
    @ApiOperation(value = "删除订单授信信息",notes = "订单授信信息管理")
    @PreAuthorize("@ss.hasPermi('manage:credit:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ocOrderCreditService.deleteOcOrderCreditByIds(ids) ? 1 : 0);
    }
}
